home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / GNU / patch_2_1.lha / patch-2.1inp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-23  |  9.8 KB  |  378 lines

  1. /* $Header: inp.c,v 2.0.1.1 88/06/03 15:06:13 lwall Locked $
  2.  *
  3.  * $Log:    inp.c,v $
  4.  * Revision 2.0.1.1  88/06/03  15:06:13  lwall
  5.  * patch10: made a little smarter about sccs files
  6.  * 
  7.  * Revision 2.0  86/09/17  15:37:02  lwall
  8.  * Baseline for netwide release.
  9.  * 
  10.  */
  11.  
  12. #include "EXTERN.h"
  13. #include "common.h"
  14. #include "util.h"
  15. #include "pch.h"
  16. #include "INTERN.h"
  17. #include "inp.h"
  18.  
  19. /* Input-file-with-indexable-lines abstract type */
  20.  
  21. static long i_size;            /* size of the input file */
  22. static char *i_womp;            /* plan a buffer for entire file */
  23. static char **i_ptr;            /* pointers to lines in i_womp */
  24.  
  25. static int tifd = -1;            /* plan b virtual string array */
  26. static char *tibuf[2];            /* plan b buffers */
  27. static LINENUM tiline[2] = {-1, -1};    /* 1st line in each buffer */
  28. static LINENUM lines_per_buf;        /* how many lines per buffer */
  29. static int tireclen;            /* length of records in tmp file */
  30.  
  31. /* New patch--prepare to edit another file. */
  32.  
  33. void
  34. re_input()
  35. {
  36.     if (using_plan_a) {
  37.     i_size = 0;
  38. #ifndef lint
  39.     if (i_ptr != Null(char**))
  40.         free((char *)i_ptr);
  41. #endif
  42.     if (i_womp != Nullch)
  43.         free(i_womp);
  44.     i_womp = Nullch;
  45.     i_ptr = Null(char **);
  46.     }
  47.     else {
  48.     using_plan_a = TRUE;        /* maybe the next one is smaller */
  49.     Close(tifd);
  50.     tifd = -1;
  51.     free(tibuf[0]);
  52.     free(tibuf[1]);
  53.     tibuf[0] = tibuf[1] = Nullch;
  54.     tiline[0] = tiline[1] = -1;
  55.     tireclen = 0;
  56.     }
  57. }
  58.  
  59. /* Constuct the line index, somehow or other. */
  60.  
  61. void
  62. scan_input(filename)
  63. char *filename;
  64. {
  65.     if (!plan_a(filename))
  66.     plan_b(filename);
  67.     if (verbose) {
  68.     say3("Patching file %s using Plan %s...\n", filename,
  69.       (using_plan_a ? "A" : "B") );
  70.     }
  71. }
  72.  
  73. /* Try keeping everything in memory. */
  74.  
  75. bool
  76. plan_a(filename)
  77. char *filename;
  78. {
  79.     int ifd, statfailed;
  80.     Reg1 char *s;
  81.     Reg2 LINENUM iline;
  82. #ifndef AMIGA
  83.     char lbuf[MAXLINELEN];
  84.     int output_elsewhere = strcmp(filename, outname);
  85. #endif /* !AMIGA */
  86.  
  87.     statfailed = stat(filename, &filestat);
  88.     if (statfailed && ok_to_create_file) {
  89.     if (verbose)
  90.         say2("(Creating file %s...)\n",filename);
  91.     makedirs(filename, TRUE);
  92.     close(creat(filename, 0666));
  93.     statfailed = stat(filename, &filestat);
  94.     }
  95. #ifndef AMIGA
  96.     /* For nonexistent or read-only files, look for RCS or SCCS versions.  */
  97.     if (statfailed
  98.     || (! output_elsewhere
  99.         && (/* No one can write to it.  */
  100.         (filestat.st_mode & 0222) == 0
  101.         /* I can't write to it.  */
  102.         || ((filestat.st_mode & 0022) == 0
  103.             && filestat.st_uid != myuid)))) {
  104.     struct stat cstat;
  105.     char *cs = Nullch;
  106.     char *filebase;
  107.     int pathlen;
  108.  
  109.     filebase = basename(filename);
  110.     pathlen = filebase - filename;
  111.  
  112.     /* Put any leading path into `s'.
  113.        Leave room in lbuf for the diff command.  */
  114.     s = lbuf + 20;
  115.     strncpy(s, filename, pathlen);
  116.  
  117. #define try(f,a1,a2) (Sprintf(s + pathlen, f, a1, a2), stat(s, &cstat) == 0)
  118.     if ((   try("RCS/%s%s", filebase, RCSSUFFIX)
  119.          || try("RCS/%s"  , filebase,         0)
  120.          || try(    "%s%s", filebase, RCSSUFFIX))
  121.         &&
  122.         /* Check that RCS file is not working file.
  123.            Some hosts don't report file name length errors.  */
  124.         (statfailed
  125.          || (  (filestat.st_dev ^ cstat.st_dev)
  126.          | (filestat.st_ino ^ cstat.st_ino)))) {
  127.         Sprintf(buf, output_elsewhere?CHECKOUT:CHECKOUT_LOCKED, filename);
  128.         Sprintf(lbuf, RCSDIFF, filename);
  129.         cs = "RCS";
  130.     } else if (   try("SCCS/%s%s", SCCSPREFIX, filebase)
  131.            || try(     "%s%s", SCCSPREFIX, filebase)) {
  132.         Sprintf(buf, output_elsewhere?GET:GET_LOCKED, s);
  133.         Sprintf(lbuf, SCCSDIFF, s, filename);
  134.         cs = "SCCS";
  135.     } else if (statfailed)
  136.         fatal2("can't find %s\n", filename);
  137.     /* else we can't write to it but it's not under a version
  138.        control system, so just proceed.  */
  139.     if (cs) {
  140.         if (!statfailed) {
  141.         if ((filestat.st_mode & 0222) != 0)
  142.             /* The owner can write to it.  */
  143.             fatal3("file %s seems to be locked by somebody else under %s\n",
  144.                filename, cs);
  145.         /* It might be checked out unlocked.  See if it's safe to
  146.            check out the default version locked.  */
  147.         if (verbose)
  148.             say3("Comparing file %s to default %s version...\n",
  149.              filename, cs);
  150.         if (system(lbuf))
  151.             fatal3("can't check out file %s: differs from default %s version\n",
  152.                filename, cs);
  153.         }
  154.         if (verbose)
  155.         say3("Checking out file %s from %s...\n", filename, cs);
  156.         if (system(buf) || stat(filename, &filestat))
  157.         fatal3("can't check out file %s from %s\n", filename, cs);
  158.     }
  159.     }
  160.     filemode = filestat.st_mode;
  161. #else /* AMIGA */
  162.     if (statfailed)
  163.     fatal2("can't find %s\n", filename);
  164.     filemode = filestat.st_mode;
  165. #endif /* AMIGA */
  166.     if (!S_ISREG(filemode))
  167.     fatal2("%s is not a normal file--can't patch\n", filename);
  168.     i_size = filestat.st_size;
  169.     if (out_of_mem) {
  170.     set_hunkmax();        /* make sure dynamic arrays are allocated */
  171.     out_of_mem = FALSE;
  172.     return FALSE;            /* force plan b because plan a bombed */
  173.     }
  174. #ifdef lint
  175.     i_womp = Nullch;
  176. #else
  177.     i_womp = malloc((MEM)(i_size+2));    /* lint says this may alloc less than */
  178.                     /* i_size, but that's okay, I think. */
  179. #endif
  180.     if (i_womp == Nullch)
  181.     return FALSE;
  182.     if ((ifd = open(filename, 0)) < 0)
  183.     pfatal2("can't open file %s", filename);
  184. #ifndef lint
  185.     if (read(ifd, i_womp, (int)i_size) != i_size) {
  186.     Close(ifd);    /* probably means i_size > 15 or 16 bits worth */
  187.     free(i_womp);    /* at this point it doesn't matter if i_womp was */
  188.     return FALSE;    /*   undersized. */
  189.     }
  190. #endif
  191.     Close(ifd);
  192.     if (i_size && i_womp[i_size-1] != '\n')
  193.     i_womp[i_size++] = '\n';
  194.     i_womp[i_size] = '\0';
  195.  
  196.     /* count the lines in the buffer so we know how many pointers we need */
  197.  
  198.     iline = 0;
  199.     for (s=i_womp; *s; s++) {
  200.     if (*s == '\n')
  201.         iline++;
  202.     }
  203. #ifdef lint
  204.     i_ptr = Null(char**);
  205. #else
  206.     i_ptr = (char **)malloc((MEM)((iline + 2) * sizeof(char *)));
  207. #endif
  208.     if (i_ptr == Null(char **)) {    /* shucks, it was a near thing */
  209.     free((char *)i_womp);
  210.     return FALSE;
  211.     }
  212.     
  213.     /* now scan the buffer and build pointer array */
  214.  
  215.     iline = 1;
  216.     i_ptr[iline] = i_womp;
  217.     for (s=i_womp; *s; s++) {
  218.     if (*s == '\n')
  219.         i_ptr[++iline] = s+1;    /* these are NOT null terminated */
  220.     }
  221.     input_lines = iline - 1;
  222.  
  223.     /* now check for revision, if any */
  224.  
  225.     if (revision != Nullch) { 
  226.     if (!rev_in_string(i_womp)) {
  227.         if (force) {
  228.         if (verbose)
  229.             say2(
  230. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  231.             revision);
  232.         }
  233.         else if (batch) {
  234.         fatal2(
  235. "this file doesn't appear to be the %s version--aborting.\n", revision);
  236.         }
  237.         else {
  238.         ask2(
  239. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  240.             revision);
  241.         if (*buf != 'y')
  242.         fatal1("aborted\n");
  243.         }
  244.     }
  245.     else if (verbose)
  246.         say2("Good.  This file appears to be the %s version.\n",
  247.         revision);
  248.     }
  249.     return TRUE;            /* plan a will work */
  250. }
  251.  
  252. /* Keep (virtually) nothing in memory. */
  253.  
  254. void
  255. plan_b(filename)
  256. char *filename;
  257. {
  258.     Reg3 FILE *ifp;
  259.     Reg1 int i = 0;
  260.     Reg2 int maxlen = 1;
  261.     Reg4 bool found_revision = (revision == Nullch);
  262.  
  263.     using_plan_a = FALSE;
  264.     if ((ifp = fopen(filename, "r")) == Nullfp)
  265.     pfatal2("can't open file %s", filename);
  266.     if ((tifd = creat(TMPINNAME, 0666)) < 0)
  267.     pfatal2("can't open file %s", TMPINNAME);
  268.     while (fgets(buf, sizeof buf, ifp) != Nullch) {
  269.     if (revision != Nullch && !found_revision && rev_in_string(buf))
  270.         found_revision = TRUE;
  271.     if ((i = strlen(buf)) > maxlen)
  272.         maxlen = i;            /* find longest line */
  273.     }
  274.     if (revision != Nullch) {
  275.     if (!found_revision) {
  276.         if (force) {
  277.         if (verbose)
  278.             say2(
  279. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  280.             revision);
  281.         }
  282.         else if (batch) {
  283.         fatal2(
  284. "this file doesn't appear to be the %s version--aborting.\n", revision);
  285.         }
  286.         else {
  287.         ask2(
  288. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  289.             revision);
  290.         if (*buf != 'y')
  291.             fatal1("aborted\n");
  292.         }
  293.     }
  294.     else if (verbose)
  295.         say2("Good.  This file appears to be the %s version.\n",
  296.         revision);
  297.     }
  298.     Fseek(ifp, 0L, 0);        /* rewind file */
  299.     lines_per_buf = BUFFERSIZE / maxlen;
  300.     tireclen = maxlen;
  301.     tibuf[0] = malloc((MEM)(BUFFERSIZE + 1));
  302.     tibuf[1] = malloc((MEM)(BUFFERSIZE + 1));
  303.     if (tibuf[1] == Nullch)
  304.     fatal1("out of memory\n");
  305.     for (i=1; ; i++) {
  306.     if (! (i % lines_per_buf))    /* new block */
  307.         if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  308.         pfatal1("can't write temp file");
  309.     if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
  310.       == Nullch) {
  311.         input_lines = i - 1;
  312.         if (i % lines_per_buf)
  313.         if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  314.             pfatal1("can't write temp file");
  315.         break;
  316.     }
  317.     }
  318.     Fclose(ifp);
  319.     Close(tifd);
  320.     if ((tifd = open(TMPINNAME, 0)) < 0) {
  321.     pfatal2("can't reopen file %s", TMPINNAME);
  322.     }
  323. }
  324.  
  325. /* Fetch a line from the input file, \n terminated, not necessarily \0. */
  326.  
  327. char *
  328. ifetch(line,whichbuf)
  329. Reg1 LINENUM line;
  330. int whichbuf;                /* ignored when file in memory */
  331. {
  332.     if (line < 1 || line > input_lines)
  333.     return "";
  334.     if (using_plan_a)
  335.     return i_ptr[line];
  336.     else {
  337.     LINENUM offline = line % lines_per_buf;
  338.     LINENUM baseline = line - offline;
  339.  
  340.     if (tiline[0] == baseline)
  341.         whichbuf = 0;
  342.     else if (tiline[1] == baseline)
  343.         whichbuf = 1;
  344.     else {
  345.         tiline[whichbuf] = baseline;
  346. #ifndef lint        /* complains of long accuracy */
  347.         Lseek(tifd, (long)baseline / lines_per_buf * BUFFERSIZE, 0);
  348. #endif
  349.         if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
  350.         pfatal2("error reading tmp file %s", TMPINNAME);
  351.     }
  352.     return tibuf[whichbuf] + (tireclen*offline);
  353.     }
  354. }
  355.  
  356. /* True if the string argument contains the revision number we want. */
  357.  
  358. bool
  359. rev_in_string(string)
  360. char *string;
  361. {
  362.     Reg1 char *s;
  363.     Reg2 int patlen;
  364.  
  365.     if (revision == Nullch)
  366.     return TRUE;
  367.     patlen = strlen(revision);
  368.     if (strnEQ(string,revision,patlen) && isspace(string[patlen]))
  369.     return TRUE;
  370.     for (s = string; *s; s++) {
  371.     if (isspace(*s) && strnEQ(s+1, revision, patlen) && 
  372.         isspace(s[patlen+1] )) {
  373.         return TRUE;
  374.     }
  375.     }
  376.     return FALSE;
  377. }
  378.